﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using HYFamilyLibDataManager;

namespace Model
{
	public partial class CableTrayFitting : System.Windows.Forms.Form
	{
		public Family SelectFamily { get; set; }

		public CableTrayFitting(UIApplication uiApp, DbCategory dbctFitting, string ctt, CableTrayShape ctShape, List<string> fittings, Transaction ts)
		{
			this.InitializeComponent();
			this.m_application = uiApp;
			this.doc = this.m_application.ActiveUIDocument.Document;
			this.dbctFitting = dbctFitting;
			this.cttName = ctt;
			this.ctShape = ctShape;
			this.ts = ts;
			this.fittings = fittings;
		}

		private string GetDBFamilyShape()
		{
			string result = "";
            if (1 == (int)this.ctShape)
			{
				result = "槽式";
			}
            else if (2 == (int)this.ctShape)
			{
				result = "梯式";
			}
			return result;
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			List<DbFamily> list = new List<DbFamily>();
			ImageList imageList = new ImageList();
			imageList.ImageSize = new Size(60, 60);
			string dbfamilyShape = this.GetDBFamilyShape();
			foreach (DbFamily dbFamily in this.dbctFitting[this.cttName][dbfamilyShape].GetAllFamilies(false, CheckType.All))
			{
				if (!this.fittings.Contains("HY_" + dbFamily.Name))
				{
					list.Add(dbFamily);
					imageList.Images.Add(AssistFunc.ResizeImage((Bitmap)dbFamily.Image, 241, 80));
				}
			}
            this.lvwCableTrays.View = System.Windows.Forms.View.LargeIcon;
			this.lvwCableTrays.LargeImageList = imageList;
			this.lvwCableTrays.BeginUpdate();
			for (int i = 0; i < list.Count; i++)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.ImageIndex = i;
				listViewItem.Tag = list[i];
				listViewItem.Text = list[i].Name;
				listViewItem.ToolTipText = list[i].Name;
				this.lvwCableTrays.Items.Add(listViewItem);
			}
			this.lvwCableTrays.EndUpdate();
		}

		private void btnLoad_Click(object sender, EventArgs e)
		{
			this.ts.Commit();
			if (this.lvwCableTrays.SelectedItems.Count > 0)
			{
				foreach (object obj in this.lvwCableTrays.SelectedItems)
				{
					DbFamily dbFamily = ((ListViewItem)obj).Tag as DbFamily;
					this.SelectFamily = dbFamily.Load(this.doc);
				}
			}
			this.ts.Start();
		}

		private UIApplication m_application;

		private Document doc;

		private List<string> fittings;

		private Transaction ts;

		private DbCategory dbctFitting;

		private string cttName;

		private CableTrayShape ctShape;
	}
}
